﻿using System;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.BLL;
using Jungle.Code.Utils;
using System.Collections.Generic;
using Jungle.Code.DAL;
using System.Data.SqlClient;

using Jungle.Prodamo.Core;

public partial class admin : BaseAdminModule
{

    protected void Page_Load(object sender, EventArgs e)
    {
        switch (Request["cmd"])
        {
            case "del":
                {
                    UserManager f = new UserManager();
                    f.UserId = Convert.ToInt32(Request["id"]);
                    f.GetById();
                    f.Vip = false;
                    f.Update();
                    Set(f.UserId, false);
                    Response.Redirect(Request.UrlReferrer.AbsoluteUri);
                    break;
                }
            case "add":
                {
                    UserManager f = new UserManager();
                    f.UserId = Convert.ToInt32(Request["id"]);
                    f.GetById();
                    f.Vip = true;
                    f.Update();
                    Set(f.UserId, true);
                    Response.Redirect(Request.UrlReferrer.AbsoluteUri);
                    break;
                }
        }

    }
    protected void Page_Init(object sender, EventArgs e)
    {

    }
    public override void FillBaseInfo()
    {
        sysName = "PriceManager";
    }
    public override void Main()
    {

    }

    protected override void Render(HtmlTextWriter writer)
    {
        UserManager firms = new UserManager();
        firms.UsertType = false;
        firms.GetByType();

        writer.AddAttribute(HtmlTextWriterAttribute.Style, "width: 80%");
        writer.RenderBeginTag(HtmlTextWriterTag.Table);
        writer.RenderBeginTag(HtmlTextWriterTag.Tr);
        writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#aa0800");
        writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "#fff");
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.Write("Список компаний");
        writer.RenderEndTag();
        writer.RenderEndTag();
        int c = 0;
        foreach (UserManager f in firms.UserList)
        {
            c++;
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            if (!f.Vip)
                writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=PriceManager&cmd=add&id=" + f.UserId.ToString());
            else
                writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=PriceManager&cmd=del&id=" + f.UserId.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(c.ToString() + ". " + f.Organization);
            if (f.Vip)
            {
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + "/Modules/PriceManager/img/vip.gif");
                    writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();

            }
            writer.RenderEndTag();

            writer.RenderEndTag();
            writer.RenderEndTag();
        }
        writer.RenderEndTag();        
        base.Render(writer);
    }
    protected void Set(int UserId, bool vip)
    {
        using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
        {
            SqlCommand command = new SqlCommand("Price_SetVip", connect);
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add("@UserId", SqlDbType.Int).Value = UserId;
            command.Parameters.Add("@Vip", SqlDbType.Bit).Value = vip;

            command.Connection.Open();
            command.ExecuteNonQuery();
            command.Connection.Close();

        }
    }
}
